//
//  SHaiXuanViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/2/24.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "SHaiXuanViewController.h"
#import "ZPSecondTableViewCell.h"
#import "ZPFirstTableViewCell.h"
typedef NS_ENUM(NSInteger,DisplayStyle) {
    TeachStyle,
    TeacherStyle
};
@interface SHaiXuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table1;
@property (weak, nonatomic) IBOutlet UITableView *table2;
@property (strong,nonatomic) NSArray *table2A;
@property (strong,nonatomic) NSArray *teacherA;
@property (strong,nonatomic) NSArray *teachA;
@property (assign) DisplayStyle style;
@end

@implementation SHaiXuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"筛选";
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    [_table1 registerNib:[UINib nibWithNibName:@"ZPFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPFirstTableViewCell"];
    [_table1 setTableFooterView:[UIView new]];
    [_table2 setTableFooterView:[UIView new]];
    [_table2 registerNib:[UINib nibWithNibName:@"ZPSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPSecondTableViewCell"];
    [self getteachlist];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getteachlist{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] POST:APIUC@"adjust/getteachlist.json" parameters:@{@"classId":_classId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            _teachA = [responseObject objectForKey:@"result"];
            [self getteacherlist];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

-(void)getteacherlist{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] POST:APIUC@"adjust/getteacherlist.json" parameters:@{@"classId":_classId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            _teacherA = [responseObject objectForKey:@"result"];
            
            [_table1 selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
            [self tableView:_table1 didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==1001) {
        return 2;
    }else{
        if (_style==TeachStyle) {
            return _teachA.count;
        }else{
            return _teacherA.count;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag==1001) {
        ZPFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPFirstTableViewCell"];
        
        if (indexPath.row==0) {
            cell.itemLab.text=@"教学点";
        }else{
            cell.itemLab.text=@"老师";
        }
        return cell;
    }else{
        NSDictionary *tmp;
        NSString *tmpS;
        if (_style==TeachStyle) {
            tmp = [_teachA objectAtIndex:indexPath.row];
            tmpS = toStr([tmp objectForKey:@"teachName"]);
        }else{
            tmp = [_teacherA objectAtIndex:indexPath.row];
            tmpS = toStr([tmp objectForKey:@"teacherName"]);
        }
        ZPSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPSecondTableViewCell"];
        cell.itemLab.text = tmpS;
        return cell;
    }
    
}
- (IBAction)resetAction:(id)sender {
    [_paramDic removeAllObjects];
    if (_resetBlock) {
        _resetBlock(@[@"teachId",@"teacherId"]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}



- (IBAction)confirmAction:(id)sender {
    NSLog(@"------%@",_paramDic);
    if (_confirmBlock) {
        _confirmBlock(_paramDic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==1001) {
        _style = indexPath.row;
        [_table2 reloadData];
        
        if (_style==TeachStyle) {
            [_teachA enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"teachId"])isEqualToString:[_paramDic objectForKey:@"teachId"]]) {
                    [_table2 selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                }
                
            }];
        }else{
            [_teacherA enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([toStr([obj objectForKey:@"teacherId"])isEqualToString:[_paramDic objectForKey:@"teacherId"]]) {
                    [_table2 selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                }
                
            }];
        }
        
        
        
    }else{
        if (_style==TeachStyle) {
            [_paramDic setObject:toStr([[_teachA objectAtIndex:indexPath.row] objectForKey:@"teachId"]) forKey:@"teachId"];
        }else{
            [_paramDic setObject:toStr([[_teacherA objectAtIndex:indexPath.row] objectForKey:@"teacherId"]) forKey:@"teacherId"];
        }
    }
}


@end
